<h1>Rechercher un projet</h1>
<script language="javascript">
/*<![CDATA[*/
var columns = new Array();

<?php

function __sort_column__($a, $b) {
	if ($a == "first_name") return -1;
	else if ($b == "first_name") return 1;
	else if ($a == $b) return 0;
	else if ($a < $b) return -1;
	else return 1;
}


$keys = array_keys($columns['types']);
usort($keys, "__sort_column__");

foreach ($keys as $id) {
	$type = $columns['types'][$id];
	if (!isset ($columns['values'][$id]) || !is_array($columns['values'][$id])) {
		echo "columns[\"$id\"] = new Column('$id', '" . $columns['labels'][$id] . "', '$type');\n";
	} else {
		echo "{\n\tvar values = new Array();\n";
		foreach ($columns['values'][$id] as $arr) {
			$id2i = str_replace("'", "\\'", $arr['id']);
			$labeli = str_replace("'", "\\'", $arr['label']);
			echo "\tvalues['$id2i'] = '$labeli'\n";
		}
		echo "\tcolumns[\"$id\"] = new Column('$id', '" . $columns['labels'][$id] . "', values);\n";
		echo "}\n";
	}

}
?>

var operators = new Array();
operators['like'] = '<?php echo str_replace("'", "\\'", Yii::t('search', 'contains')); ?>';
operators['='] = '<?php echo str_replace("'", "\\'", Yii::t('search', 'equals')); ?>';
operators['!='] = '<?php echo str_replace("'", "\\'", Yii::t('search', 'different of')); ?>';
operators['>'] = '<?php echo str_replace("'", "\\'", Yii::t('search', 'greater than')); ?>';
operators['<'] = '<?php echo str_replace("'", "\\'", Yii::t('search', 'lower than')); ?>';
operators['starts'] = '<?php echo str_replace("'", "\\'", Yii::t('search', 'starts with')); ?>';
operators['ends'] = '<?php echo str_replace("'", "\\'", Yii::t('search', 'ends with')); ?>';

function addFilter(){
	var search = $('#search');
	var item = new SearchItem(search[0]);
  	search[0].appendChild(item.search_div);
  	return item;
}

/*]]>*/
</script>

<?php


//Constants::print_var($columns, false);
//foreach ($columns['types'] as $id => $type) {
//if (!isset ($columns['values'][$id]) || !is_array($columns['values'][$id])) {
//	echo "<pre>!array($id)</pre>";
//}else{
//	Constants::print_var($columns['values'][$id], false);
//}
//}
//Constants::print_var("");
?>

<?php echo CHtml::beginForm(array('', 'ts'=>$ts), 'post', array('id' => 'SearchForm')); ?>
<input type="hidden" value="0" id="nbSearchArea" name="SearchForm[nb]" />
<div class="yiiForm">
	<div id="search">

	</div><!-- search -->
<?php


echo CHtml::link(CHtml::image('images/add.png', 'Add a filter') . "&nbsp;" . Yii::t('search', 'Add a filter'), '#', array (
	'onclick' => "addFilter()",
	'border' => '0',
	'class' => 'search_link'
));
?>

	<div class="action">
	<?php echo CHtml::submitButton(Yii::t('views', 'Search')); ?>
	</div><!-- action -->
</div><!-- yiiForm -->

<?php

if ($results !== null && isset ($results)) {
	if (count($results) <= 0) {
		echo Yii::t('search', "No project found");
	} else {
?>
<br /><br />
<table class="dataGrid">
  <thead>
  <tr>
    <th><?php echo $sort->link('project_id', Yii::t('search', 'Project #')); ?></th>
    <th><?php echo $sort->link('activity', Yii::t('database', 'Activity')); ?></th>
    <th><?php echo $sort->link('route_mnemonic', Yii::t('search', 'Route')); ?></th>
    <th><?php echo $sort->link('first_name', Yii::t('database', 'First Name')); ?></th>
    <th><?php echo $sort->link('last_name', Yii::t('database', 'Last Name')); ?></th>
    <th><?php echo $sort->link('birth_date', Yii::t('database', 'Birth Date')); ?></th>
    <th><?php echo $sort->link('email', Yii::t('database', 'Email')); ?></th>
    <th><?php echo $sort->link('referent', Yii::t('database', 'Referent')); ?></th>
    <th><?php echo $sort->link('adviser_initials', Yii::t('search', 'Adviser')); ?></th>
    <th><?php echo $sort->link('city_name', Yii::t('database', 'City')); ?></th>
    <th><?php echo $sort->link('postal_code', Yii::t('database', 'Postal Code')); ?></th>
  </tr>
  </thead>
  <tbody>
<?php foreach($results as $n=>$model): ?>
  <tr class="<?php echo $n%2?'even':'odd';?>">
    <td><?php echo CHtml::link($model->project_id,array('show','id'=>$model->project_id), array('target' => 'project_'.$model->project_id)); ?></td>
    <td><?php echo CHtml::encode($model->activity); ?></td>
    <td><?php echo CHtml::encode($model->route_mnemonic); ?></td>
    <td><?php echo CHtml::encode($model->first_name); ?></td>
    <td><?php echo CHtml::encode($model->last_name); ?></td>
    <td><?php echo CHtml::encode($model->birth_date); ?></td>
    <td><?php echo CHtml::encode($model->email); ?></td>
    <td><?php echo CHtml::encode($model->referent); ?></td>
    <td><?php echo CHtml::encode($model->adviser_initials); ?></td>
    <td><?php echo CHtml::encode($model->city_name); ?></td>
    <td><?php echo CHtml::encode($model->postal_code); ?></td>
  </tr>
<?php endforeach; ?>
  </tbody>
</table>
<br/>
<?php


		$this->widget('CLinkPager', array (
			'pages' => $pages
		));
	}
}
?>

<?php

echo CHtml::endForm();

$cs = Yii::app()->clientScript;
$cs->registerScriptFile(Yii::app()->baseUrl . '/js/masks.js', CClientScript::POS_HEAD);
$cs->registerScriptFile(Yii::app()->baseUrl . '/js/search.js', CClientScript::POS_HEAD);
$script =<<<JS
SearchItem.operators = operators;
SearchItem.columns = columns;
SearchItem.form_name = 'SearchForm';
SearchItem.form=$('#SearchForm')[0];
JS;

$cs->registerScript('search_item_init', $script);

if (isset ($_POST['SearchForm']) && isset ($_POST['SearchForm']['columns'])) {
	// realoding form
	$script = "SearchItemAPI.lang = '".Yii::app()->user->language."';\n";
	$script .= "var item = null;\n";
	foreach ($_POST['SearchForm']['columns'] as $id => $column_name) {
		$script .= "item = addFilter();\n";
		$script .= "jQuery(item.select_column).val('" . str_replace("'", "\\'", $column_name) . "');\n";
		$script .= "item.columnChange();\n";

		if (isset ($_POST['SearchForm']['operator'][$id]))
			$script .= "jQuery(item.select_operator).val('" . str_replace("'", "\\'", $_POST['SearchForm']['operator'][$id]) . "');\n";

		if (isset ($_POST['SearchForm']['value'][$id]))
			$script .= "jQuery(item.value_item).val('" . str_replace("'", "\\'", $_POST['SearchForm']['value'][$id]) . "');\n";

	}
	$cs->registerScript('search_form_init', $script);
}

//$this->widget('application.extensions.calendar.SCalendar', array (
//	'registerOnly' => true
//));

$basePath=Yii::getPathOfAlias('zii.vendors.jui');
$baseUrl=Yii::app()->getAssetManager()->publish($basePath);
$cs=Yii::app()->getClientScript();
$cs->registerCssFile($baseUrl.'/css/base/jquery-ui.css');
$cs->registerCoreScript('jquery');
$cs->registerScriptFile($baseUrl.'/js/jquery-ui.min.js', CClientScript::POS_END);
$cs->registerScriptFile($baseUrl.'/js/jquery-ui-i18n.min.js', CClientScript::POS_END);


//
//$this->widget('zii.widgets.jui.CJuiDatePicker', array(
//      'name'=>'publishDate',
//      // additional javascript options for the date picker plugin
//      'options'=>array(
//          'showAnim'=>'fold',
//      ),
//      'htmlOptions'=>array(
//          'style'=>'height:20px;'
//      ),
//  ));

?>